package org.example;

import java.util.Arrays;

/**
 * @AUTHOR 尹书岿
 * @DATE 2024/12/9 14:47
 * @DESCRIBE
 */
public class StringDemo {
    public static void main(String[] args) {
        String str = "hello,world,cpdd";
        System.out.println(str.length());
        //1.遍历字符串charAt
//        for (int i = 0; i<str.length();i++) {
//            System.out.println(str.charAt(i));
//        }

        //2.字符串转字符数组
        char[] charArr = str.toCharArray();
//        for (char c : charArr) {
//            System.out.println(c);
//        }
//        for (int i = 0; i< charArr.length; i++) {
//            System.out.println(str.charArr(i));
//        }

        //3.分割
        String[] arr2 = str.split(",");
        System.out.println(Arrays.toString(arr2));

        //4.拼接
        String arr3 = "hello" + " world";
        System.out.println(arr3);

        //5.比较 arr3和arr4/5是否相等 arr3和arr4指向的内存地址不同
        String arr4 = "hello world";
        String arr5 = new String("hello world");
        if (arr3.equals(arr4)){
            System.out.println("相等");
        }else {
            System.out.println("不相等");
        }

        //6.包含
        System.out.println(arr4.contains("world"));

        //7.替换
        String newArr4 = arr4.replace("world","java");
        System.out.println(newArr4);

//        8.求子字符串 前闭后开
        String arr6 = "hello world java111";
        String newArr6 = arr6.substring(0,4);
        System.out.println(newArr6);

        //9.indexOf（）
        int index = arr6.indexOf("java");
        System.out.println(index); //返回字符串第一次出现位置的下标

        //10.toUpperCase 小写转大写 toUpperCase 大写转小写


        //11.各字符转化 数字转字符串
        int k = 21;
        System.out.println(String.valueOf(k)+"cpdd");

        //12.字符串转数字
        String str2 = "10";
        int num1 = Integer.parseInt(str2);
        System.out.println(num1+56);

        //13.字符转数字 ascii码
        char c = 65;
        System.out.println(c);
        char c2 = 'A' + 32;
        System.out.println(c2);
        //字符转数字
        int c3 = '6' - '0';
        System.out.println(c3);
    }
}
